//堆排序
//
void sorttest()
{
	int arr[] = { 15,18,19,37,25,28,34,65,27,49 };
	int n = sizeof(arr) / sizeof(arr[0]);
	Heap php;
	//建堆
	HeapCreat(&php, arr, n);

	for (int end = n - 1; end > 0; end--)
	{
		Swap(&php.a[0], &php.a[end]);
		AdjustDown(0, php.a, end);
	}
}




void TopkTest()
{
	//造数据
	srand((unsigned int)time(0));

	FILE* pin = fopen("C:\\Users\\29519\\Desktop\\text.txt", "w");
	if (pin == NULL)
	{
		perror("fopen");
		exit(-1);
	}
	//存入N个数
	int i = 0;
	int n = 10;
	while (i < n)
	{
		int val = rand() % 100;
		fprintf(pin, "%d\n", val);
		i++;
	}

	fclose(pin);


	//选topk
	//取数据建K个数的小堆
	FILE* pout = fopen("C:\\Users\\29519\\Desktop\\text.txt", "r");
	int k = 5;
	int* topk = (int*)malloc(sizeof(int) * k);

	for (int i = 0; i < k; i++)
	{
		fscanf(pout, "%d", &topk[i]);
	}

	for (int i = (k - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(i, topk, k);
	}

	//开始比较
	int val = 0;
	while (fscanf(pout, "%d", &val) != EOF)
	{
		if (val > topk[0])
		{
			topk[0] = val;
			AdjustDown(0, topk, k);
		}
	}



	for (int i = k - 1; i >= 0; i--)
	{
		printf("%d  ", topk[i]);
	}

	fclose(pout);
}



int main()
{
	TopkTest();
	return 0;
}
